<?php

namespace App\Models\SubAccount;

use App\Models\Business\RecordDataColumn;
use App\Models\Business\SubAccountSource;
use App\Models\CustomerManager;
use App\Models\Department;
use App\Models\ShopManager;
use App\Models\SystemUser;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use App\Models\ShopDetail;

class SubAccount extends Model
{

    protected $guarded=[];
    protected $appends=['customer'];
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y/m/d H:i:s');
    }

    public function admin(){
        return $this->belongsTo(SystemUser::class,'create_admin_id','id');
    }

    public function shop(){
        return $this->belongsTo(ShopDetail::class,'shop_id','id');
    }
    //自定义数据字段模板
    public function template(){
        return $this->belongsTo(RecordDataColumn::class,'template_id','id');
    }

    public function source(){
        return $this->belongsTo(SubAccountSource::class,'source_id','id');
    }

    public function staffBinds(){
        return $this->hasMany(SubAccountStaffBind::class,'sub_account_id','id');
    }

    public function staff(){
        return $this->belongsToMany(SystemUser::class,'sub_account_staff_binds','sub_account_id','staff_id','id')->withPivot('is_backup');;
    }

    //
    public function repayLogs(){
        return $this->hasMany(SubAccountRepayLog::class,'sub_account_id','id');
    }
    //
    public function department(){
        return $this->belongsTo(Department::class,'department_id');
    }
    //
    public function department1(){
        return $this->belongsTo(Department::class,'department1_id');
    }
    //
    public function department2(){
        return $this->belongsTo(Department::class,'department2_id');
    }
    //
    public function department3(){
        return $this->belongsTo(Department::class,'department3_id');
    }
    //客户登录名称
    protected function customer(): Attribute
    {
        return new Attribute(
            get: fn () => $this->shop->customer->customer_login_name??'',
        );
    }


}
